package at.fhh.se.tjr.client.view;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.util.Iterator;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import at.fhh.se.tjr.client.controller.Controller;
import at.fhhagenberg.se.tjr.protocol.Message;

public class MessagesPanel extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6858960912260294738L;

	private JTable table = new JTable();

	private DefaultTableModel model = new DefaultTableModel();
	private int lastIndex = 0;
	
	public MessagesPanel() {
		this.setLayout(new GridLayout(1,1));
		model.addColumn("MessageType");
		model.addColumn("MoteId");
		this.table.setFillsViewportHeight(true);
		JScrollPane scrolli = new JScrollPane(table);
		scrolli.setVisible(true);
		this.add(scrolli);
		table.setVisible(true);
		table.setModel(model);
		System.out.println(table.getModel().getColumnCount());
		table.setBackground(Color.WHITE);
		table.setFont(Controller.FONT);
	}

	public void addMessage(Message msg) {
		Object[] row = { msg.getClass().getSimpleName(), msg.getMoteId() };
		model.addRow(row);
		this.repaint();
		table.repaint();
	}

	public void removeMessage(){
		model.removeRow(0);
		this.repaint();
		table.repaint();
	}
	
	
//	public void refreshTable(){
//		System.out.println("refresh");
//		table.removeAll();
//		Iterator<Message> it = Controller.getInstance().getMessageQueue().iterator();
//		while ( it.hasNext()){
//			Message m = it.next();
//			Object[] row = {m.getClass().getSimpleName(), m.getMoteId()};
//			model.addRow(row);
//		}
//		table.repaint();
//		this.repaint();
//	}
}
